﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SILAS.Entidades;
using SILAS.BL;
using Microsoft.Practices.Unity;

namespace SILAS.UI.Controllers
{
    public class SAPController : Controller
    {
        //
        // GET: /SAP/
        [Dependency]
        public ISapBL SAPBL { get; set; }

        [Dependency]
        public SectorBL SectorBL { get; set; }

        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Index(int id)
        {
            var idsec = id;
            ViewBag.id = idsec;
            var listi = SAPBL.ListarSapByIdsector(idsec);

            return PartialView(listi);
        }
       
        //
        // GET: /SAP/Details/5
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult AsignarSAP(int id)
        {
            var iid = id;
            ViewBag.iid = iid;
            var list = SAPBL.ListarSapByIdsector(iid);
            ViewData["SapID"]= new SelectList (list, "Id", "Nombre");
            return PartialView();
        }

       [HttpPost]
       [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult AsignarSAP(int idSap, int idsector)
        {

            try
            {   
               int idSector = idsector;
               ViewBag.iid = idSector;
                // TODO: Add update logic here
                SAPBL.registrarSapSector(idSap,idSector);

                var result = new
                {
                    msg = "Sap se agrego correctamente",
                    exito = true
                };

                return Json(result, JsonRequestBehavior.AllowGet);

                //return RedirectToAction("ListarEmpleados");
            }
            catch (SystemException ex)
            {

                var result = new
                {
                    msg = ex.Message,
                    exito = false
                };

                return Json(result, JsonRequestBehavior.AllowGet);
                //return View();
            }

        }
        
        public ActionResult GetSAPbycrit(int idSector, string term )
        {
            var lista = from e in SAPBL.ListaSAPByCriterio(term, idSector)
                        select new
                        {
                            value = e.Id,
                            label = e.Nombre,
                            SAP = new
                            {
                                Id = e.Id,
                                Nombre = e.Nombre,
                                FecConstruccion = e.FecConstruccion,
                                TipoSAP = e.TipoSAP.Descripcion,

                            }

                        };

            return Json(lista, JsonRequestBehavior.AllowGet);
        }
         //
        // GET: /SAP/Create
        [HttpGet]
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Create(int id)
        {
            int idsector = id;
            ViewBag.id = id;
            //ViewBag.listSaps = SAPBL.ListarSapByIdsector(idsector);
            var lisinejecut = SAPBL.getinstEjecutora();
            var lisinisuperviso = SAPBL.getentiSupervisora();
            var tiposap = SAPBL.getiposap();


            //ViewData["IdSector"] = new SelectList(listSaps, "IdSAP", "Nombre");
            ViewData["IdInstSupervisora"] = new SelectList(lisinisuperviso, "IdInstSupervisora", "Descripcion");
            ViewData["IdInstEjecutora"] = new SelectList(lisinejecut, "IdInstEjecutora", "Descripcion");
            ViewData["IdTipoSAP"] = new SelectList(tiposap, "Id", "Descripcion");

          
            /* var fuente = new FuenteAgua();
            var sap = new SAP();
             
            sap.FuenteAguas.Add(fuente); 

            var sector = new Sector() { SAPs = sap };
            */

            return PartialView();
        }

        //
        // POST: /SAP/Create

        [HttpPost]
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Create (SAP Sap, FormCollection _form)
        {
            try
            {

                // TODO: Add insert logic here
                SAPBL.registrarSap(Sap);

                var result = new
                {
                    msg = "Sap Creada Correctamente",
                    exito = true
                };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (SystemException ex)
            {
                var result = new
                {
                    msg = ex.Message,
                    exito = false
                };

                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }



        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult RegistrarSap()
        {
            // int idsector = 1;
            //ViewBag.listSaps = SAPBL.ListarSapByIdsector(idsector);
            var lisinejecut = SAPBL.getinstEjecutora();
            // ViewBag.lisinisuperviso = SAPBL.getentiSupervisora();
            var lisinisuperviso = SAPBL.getentiSupervisora();
            var tiposap = SAPBL.getiposap();


            //ViewData["IdSector"] = new SelectList(listSaps, "IdSAP", "Nombre");
            ViewData["IdInstSupervisora"] = new SelectList(lisinisuperviso, "IdInstSupervisora", "Descripcion");
            ViewData["IdInstEjecutora"] = new SelectList(lisinejecut, "IdInstEjecutora", "Descripcion");
            ViewData["IdTipoSAP"] = new SelectList(tiposap, "Id", "Descripcion");

            //que lo revise el profe si esta bien lo de abajito
            
            /* var sap = new SAP();
             var fuente = new FuenteAgua();
             sap.FuenteAguas.Add(fuente);
            */
            return PartialView();
        }

        //
        // POST: /SAP/Create

        [HttpPost]
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult RegistrarSap(SAP Sap, FormCollection _form)
        {
            try
            {

                // TODO: Add insert logic here
                SAPBL.registrarSap(Sap);

                var result = new
                {
                    msg = "Sap Creada Correctamente",
                    exito = true
                };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (SystemException ex)
            {
                var result = new
                {
                    msg = ex.Message,
                    exito = false
                };

                return Json(result, JsonRequestBehavior.AllowGet);
            }
        }

        //
        // GET: /SAP/Edit/5
        
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /SAP/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /SAP/Delete/5
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Delete(int id)
        {
            SAPBL.eliminarSap(id);
            return RedirectToAction("Index");
        }

        //
        // POST: /SAP/Delete/5

        [HttpPost]
        [Authorize(Roles = "AdministradorSistema,AdministradorInformacion")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
